package com.heima.kafka.sample;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.time.Duration;
import java.util.Collections;
 import java.util.Properties;

/**
 * @Author: xsz
 * @DateTime: 2024/3/1 0:23
 **/
public class ConsumerQuickStart {

    public static void main(String[] args) {
        //kafka的配置信息
        Properties prop = new Properties();
        prop.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.200.130:9092");
        prop.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        prop.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");


        //创建消费者对象
        prop.put(ConsumerConfig.GROUP_ID_CONFIG,"group1");
        KafkaConsumer<String,String> consumer  = new KafkaConsumer<String, String>(prop);
        //订阅主题
        consumer.subscribe(Collections.singletonList("topic-first"));
        // 拉取消息
        while (true){
            System.out.println("监听");
            ConsumerRecords<String, String> consumerRecords = consumer.poll(Duration.ofMillis(1000));
            for (ConsumerRecord<String, String> consumerRecord : consumerRecords) {
                System.out.println(consumerRecord.key());
                System.out.println(consumerRecord.value());
            }
        }

    }

}
